package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleService vehicleService;
    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        List<GeofenceVO> geofenceVOS = geofenceMapper.selectGeofence(geofenceQuery);
        //判断该集合是否为空
        if (geofenceVOS==null||geofenceVOS.size()==0)
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        //现在需要补充车辆信息：车辆总数  可用车辆
        //遍历geofenceVOS集合，取出单个围栏信息对象，进行操作
        for (int i = 0; i < geofenceVOS.size(); i++) {
            GeofenceVO geofenceVO = geofenceVOS.get(i);
            //查询当前围栏信息对象上绑定的车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            //查询条件为围栏id
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            //进行查询
            List<VehicleVO> vehicleVOS = vehicleService.selectVehicle(vehicleQuery);
            if (vehicleVOS==null||vehicleVOS.size()==0)
                throw new ServiceException(StatusCode.USERNAME_ERROR);
            //获取绑定车辆的总数
            int totalNum = vehicleVOS.size();
            //定义一个统计可用车辆的参数
            int availableNum = 0;
            //vehicleVOS集合，如果对应的车辆的状态为空闲，就让availableNum+1
            for (VehicleVO vehicleVO : vehicleVOS) {
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //给当前对象设置可用车辆数，如果可用车辆数>0,则有可用车辆，否则可用车辆设置为0
            if(availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else {
                geofenceVO.setAvailableNum(0);
            }
            //设置围栏对象的总车辆
            geofenceVO.setTotalNum(totalNum);
            //将车辆集合对象存入到围栏对象中
            geofenceVO.setVehicleList(vehicleVOS);
        }
        return geofenceVOS;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        //传数据到mapper层是geofence类型，需要初始化一个Genfence对象
        Geofence geofence = new Geofence();
        //geofenceParam赋值给geofence,同名属性并且同类型的进行赋值
        BeanUtils.copyProperties(geofenceParam,geofence);
        //针对围栏位置的信息，geofenceParam是HashMap类型的
        //但是Geofence需要的是String类型的，需要进行转换
        //利用ObjectMapper工具进行转换
        HashMap<String, String> position = geofenceParam.getPosition();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String positionStr = objectMapper.writeValueAsString(position);
            //判断是新增还是修改
            if(geofenceParam.getId()!=null){//修改
                geofenceMapper.update(geofence);
            }else{//新增
                geofence.setStatus("1");//默认新增的围栏的状态为启用 1   禁用 0
                geofence.setPosition(positionStr);//位置信息
                geofence.setCreateTime(new Date());//创建时间默认为系统时间
                geofenceMapper.insert(geofence);
            }
        } catch (JsonProcessingException e) {//JSON转化异常，抛出自己写的业务异常
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        //先查询该围栏中有没有绑定的车辆，车辆模块中查询车辆的方法需要的VehicleQuery对象
        VehicleQuery vehicleQuery = new VehicleQuery();
        //将围栏id赋值到vehicleQuery对象中
        vehicleQuery.setGeofenceId(id);
        //利用VehicleService中的方法进行查询
        List<VehicleVO> vehicleVOS = vehicleService.selectVehicle(vehicleQuery);
        //判断vehicleVOS中是否有值
        if(vehicleVOS!=null && vehicleVOS.size()>0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            //围栏中没有车辆，才能删除围栏信息
            geofenceMapper.deleteById(id);
        }
    }
}
